package com.bluemsun.naturallanguageprocessing.tools;

import com.bluemsun.naturallanguageprocessing.common.TagCommon;
import com.bluemsun.naturallanguageprocessing.dao.QuestionMapper;
import com.bluemsun.naturallanguageprocessing.dao.TagMapper;
import com.bluemsun.naturallanguageprocessing.entity.Question;
import com.bluemsun.naturallanguageprocessing.vo.QuestionVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

//TODO：定时任务为问题划归标签
@Component
@ConfigurationProperties(prefix = "allows")
public class QuestionTag {

    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private TagMapper tagMapper;

    private static final Logger logger = LoggerFactory.getLogger(QuestionTag.class);

    @Transactional
    public void setQuestionTags(){
        List<Question> allQuestions = questionMapper.getAllQuestions();
        Question question = null;
        String title = null;
        Integer integer = null;
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < allQuestions.size(); i++) {
            arrayList.clear();
            question = allQuestions.get(i);
            title = question.getTitle();
            for (int j = 0; j < TagCommon.strings.length; j++) {
                if(title.contains(TagCommon.strings[j])){
                    arrayList.add(TagCommon.strings[j]);
                }
            }
            if (arrayList.size()!=0){
                try{
                    integer = tagMapper.insertQuestionTag(arrayList, question);
                }catch (RuntimeException e){
                    logger.error(e.getMessage());
                }
            }
        }
    }

}
